package com.jt.controller;

import com.jt.pojo.Role;
import com.jt.pojo.User;
import com.jt.service.RoleService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Happy Nest-ZhangJian
 * @Date 2021/12/8
 * @Update 2021/12/8 16:37
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public SysResult getItemList(PageResult pageResult){//3
        //3+2(总记录数,分页结果)
        pageResult  = roleService.getRoleList(pageResult);
        return SysResult.success(pageResult);//5
    }

    @PutMapping("/updateRoleStatus")
    public SysResult updateRoleStatus(@RequestBody Role role){
        roleService.updateRoleStatus(role);
        return SysResult.success();
    }

    @PostMapping("/saveRole")
    public SysResult saveItem(@RequestBody Role role){
        roleService.saveItem(role);
        return SysResult.success();
    }

    @GetMapping("/{roleId}")
    public SysResult findRoleById(@PathVariable Integer roleId){
        Role role = roleService.findRoleById(roleId);
        return SysResult.success(role);
    }

    @PutMapping("/updateRole")
    public SysResult updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return SysResult.success();
    }

    @DeleteMapping("/deleteRoleById")
    public SysResult deleteRoleById(Integer id){
        roleService.deleteRoleById(id);
        return SysResult.success();
    }

    @PostMapping("/login")
    public SysResult login(@RequestBody Role role){

        //返回值一个字符串 token
        String token = roleService.login(role);
        if(token == null){
            return SysResult.fail(); //201
        }
        return SysResult.success(token); //200
    }

}
